package com.xnew.manager.maintainPlan.controller;

import com.xnew.aes.LogParameter;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.manager.maintainPlan.entity.ElMaintainPlan;
import com.xnew.manager.maintainPlan.service.ElMaintainPlanService;
import com.xnew.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description: 维保计划
 * @Author: Y70
 * @date 2022/10/24
 */
@RestController
@RequestMapping("/maintainPlan")
public class ElMaintainPlanController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private ElMaintainPlanService elMaintainPlanService;

    @LogParameter(key = "", desc = "维保计划列表")
    @RequestMapping(value = "/queryMaintainPlanList", method = RequestMethod.POST)
    public DataOutput queryMaintainPlanList(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        try{
            List<ElMaintainPlan> elMaintainPlanDetails = elMaintainPlanService.queryMaintainPlanList(input);
            out = MybatisParam.getOutput(ConvertEntityUtil.convertListData(elMaintainPlanDetails));
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            out.setCode(-94001);
            out.setMsg("列表查询出错！");
        }
        return out;
    }

    @LogParameter(key = "", desc = "创建电梯时调用的维保计划创建接口")
    @RequestMapping(value = "/createMaintainPlan", method = RequestMethod.POST)
    public DataOutput createMaintainPlan(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        try{
            Map param = input.getParam();
            if (param.get("elId") == null || StringUtils.isEmpty(param.get("elId").toString()) ||
                param.get("contractBeforeSignMaintDate") == null || StringUtils.isEmpty(param.get("contractBeforeSignMaintDate").toString())||
                param.get("contractEndDate") == null || StringUtils.isEmpty(param.get("contractEndDate").toString())||
                param.get("maintOrgId") == null || StringUtils.isEmpty(param.get("maintOrgId").toString())){
                out.setCode(-93001);
                out.setMsg("缺少必填参数！");
                return out;
            }
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            out = elMaintainPlanService.createMaintainPlan(param, userInfo);
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            out.setCode(-93001);
            out.setMsg("操作失败！");
        }
        return out;
    }
}
